﻿using UnityEngine;
using System.Collections;

public class Block : TouchScript {
	
	Vector3 Velocity = Vector3.zero;
	GameObject[] Bars;
	GameObject targetBar;
	public Transform parent;
	bool Selected = false;
	private int selectedFingerId = -1;
	public GameObject particlePreFab;
	// Use this for initialization
	void Start () {
		Velocity.y = -Random.Range(1,4);
		int colour = Random.Range(1,4);
		this.transform.parent = parent.GetChild(0).GetChild(1);
		Bars = GameObject.FindGameObjectsWithTag("Bar");
		switch(colour)
		{
		case 1:
			this.renderer.material.color = Color.green;
			targetBar = parent.GetChild(0).GetChild(0).GetChild(1).gameObject;
			break;
		case 2:
			this.renderer.material.color = Color.blue;
			targetBar = parent.GetChild(0).GetChild(0).GetChild(0).gameObject;
			break;
		case 3:
			this.renderer.material.color = Color.red;
			targetBar = parent.GetChild(0).GetChild(0).GetChild(2).gameObject;
			break;
		}
		transform.position = new Vector3(Bars[Random.Range(0,3)].transform.position.x,transform.position.y+5,transform.position.z);

	}
	float RoundToNearestColour(float hitX)
	{
		float closest = 1000;
		float temp = 0;
		foreach (var colourBar in Bars) {
			float distance = colourBar.transform.position.x - hitX;
			
			if(distance*distance < closest*closest)
			{ temp = colourBar.transform.position.x; closest = distance; }
		}
		return temp;
	}
	void OnMouseDrag()
	{
		Vector3 hit = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		this.transform.position = new Vector3(hit.x,this.transform.position.y,this.transform.position.z);
		
	}
	void OnMouseUp()
	{
		Vector3 hit = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		this.transform.position = new Vector3(RoundToNearestColour(hit.x),this.transform.position.y,this.transform.position.z);

	}
	public override void Move (Vector3 pos, TouchPhase phase, int fingerId)
	{
		//First Click on Object
		if(phase == TouchPhase.Began && this.collider2D.OverlapPoint(new Vector3(pos.x,pos.y)))
		{
			Debug.Log("Touched " + fingerId);
			Selected = true;
			selectedFingerId = fingerId;
		}
		else if(phase == TouchPhase.Ended && selectedFingerId == fingerId)
		{
			Debug.Log("Ended " + fingerId);
			Selected = false;
			selectedFingerId = -1;
			this.transform.position = new Vector3(RoundToNearestColour(pos.x),this.transform.position.y,this.transform.position.z);
		}
		//Dragging the Object Add the logic here
		if(Selected && selectedFingerId == fingerId)
			this.transform.position = new Vector3(pos.x,this.transform.position.y,this.transform.position.z);
		
	}
	// Update is called once per frame
	void Update () {
		transform.position += Velocity * Time.deltaTime;
		if(transform.position.y < targetBar.transform.position.y - targetBar.renderer.bounds.size.y/2)
		{
			if(transform.position.x == targetBar.transform.position.x)
			{

				GameObject explosion = (GameObject)Instantiate(particlePreFab,this.transform.position,this.transform.rotation);
				explosion.particleSystem.startColor = this.renderer.material.color;
				Destroy(explosion,1);

			}
			
			DestroyImmediate(this.gameObject);
		}
		
		
		
	}
}
